package com.dong.demo222.utils;

import com.dong.demo222.config.LoginUser;
import com.dong.demo222.model.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;

public class SecurityUtils {
    public static User getLoginUser()
    {
        try
        {
            return  (User)getAuthentication().getPrincipal();
        }
        catch (Exception e)
        {
            throw new RuntimeException("获取用户信息异常");
        }
    }

    public static boolean isAdmin(Long userId)
    {
        return userId != null && 1L == userId;
    }

    /**
     * 获取Authentication
     */
    public static Authentication getAuthentication()
    {
        return SecurityContextHolder.getContext().getAuthentication();
    }
}
